<?php
/**
 * 
 * @author natanael
 * controlador rn Estatus 
 * 
 */
class rnestatus extends Controller{
 
	public function __construct()
	{
	    parent::Controller();
	    $this->load->model(array('Mrnestatus'));
	}
	
	public function index()
	{
	    $this->load->view('main', array('content_page' => 'rnestatus_list','menutop' => 0));
	}
	
	public function add()
	{
	   $this->load->view('main', array('content_page' => 'rnestatus_add','menutop' => 0));
	}
	
    public function edit($id)
	{
	   $data = $this->Mrnestatus->LoadEstatusById(intval($id));	
	   $this->load->view('main', array('content_page' => 'rnestatus_edit','menutop' => 0,'data'=>$data));
	}
	
	public function delete()
	{
	   $ids = $this->input->post('delete_rows');
	   
	   if ($this->Mrnestatus->delete($ids) === true) {
	   	   
	   }else{
	         
	   } 
	   
	}
	
	public function data()
	{
	    $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'Id_Estatus'; // Columna para el order by
		$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		$limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
		$start = $limit * $page - $limit;
		if($start < 0) $start = 0;
  
		    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		    header("Cache-Control: no-cache, must-revalidate" );
		    header("Pragma: no-cache" );

		$records = $this->Mrnestatus->loadEstatus($start, $limit, $sortname, $sortorder);

		$totalRecords = count($records);

		$totalPages = ceil($totalRecords/$limit);
		if ($page > $totalPages) $page = $totalPages;

		$json_output = array(
		'page'		=> $page,
		'total'		=> $totalPages,
		'records'	=> $totalRecords,
		'rows'		=> array()
		);

		if($records != false) 
		{
			foreach($records as $objetos){
	
				$arr = $json_output['rows'][] = array(
				       'IdEstatus'         => $objetos->getIdEstatus(),
				       'Detalles'          => $objetos->getDescripcionEstatus()
				   
				);
			}
		}

		echo json_encode($json_output);
	}
	
	public function save()
	{	
	    $data = array(
			          'Descripcion_Estatus'           => strtoupper($this->input->post('description'))
			         );
		
		if ($this->input->post('idEstatus') == false) {
				 
			if ($this->Mrnestatus->crear($data) == true){
			     $this->session->set_flashdata('error_title', 'Se a Creado el Estatus');
			 }else{
			 	$this->session->set_flashdata('error_title', 'Huvo un error');
			 } 
			
		}else{
			           
			   if ($this->Mrnestatus->edit($data,$this->input->post('idEstatus')) == true){
			        $this->session->set_flashdata('error_title', 'Se Edit el Estatus');
			   }else{
			 	$this->session->set_flashdata('error_title', 'Huvo un error');
			   }        
			
		}
		
		redirect('rnestatus');
	
	}

}